/*
 *  Blazon: A user datagram protocol re-braodcasting daemon.
 *  tools.c: Useful functions and linked list support.
 *
 *  Copyright 2012 Blazon development team
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Code derived from ratbox-services.
 *
 *  Copyright (C) 2003-2007 Lee Hardy <leeh@leeh.co.uk>
 *  Copyright (C) 2003-2012 ircd-ratbox development team
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *  1.Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  2.Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  3.The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 *  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

#include "std-include.h"

#include "tools.h"
#include "blazon.h"

/* my_calloc()
 *   wrapper for calloc() to detect out of memory
 */
void *
my_calloc(size_t nmemb, size_t size)
{
	void *p;

	p = calloc(nmemb, size);

	if (p == NULL)
		die("out of memory");

	return p;
}

void *
my_realloc(void *p, size_t size)
{
	p = realloc(p, size);

	if (p == NULL)
		die("Out of memory");

	return p;
}

/* my_free()
 *   wrapper for free() that checks what we're freeing exists
 */
void
my_free(void *p)
{
	if (p != NULL)
		free(p);
}


char *
my_strndup(const char *x, size_t y)
{
    char *ret = my_malloc(y);
    strlcpy(ret, x, y);
    return (ret);
}

char *
my_strdup(const char *x)
{
    char *ret = my_malloc(strlen(x) + 1);
    strcpy(ret, x);
    return (ret);
}



/* get_duration()
 *   converts duration in seconds to a string form
 *
 * inputs   - duration in seconds
 * outputs  - string form of duration, "n day(s), h:mm:ss"
 */
const char *
get_duration(time_t seconds)
{
	static char buf[STR_BUFSIZE];
	int days, hours, minutes;

	days = (int) (seconds / 86400);
	seconds %= 86400;
	hours = (int) (seconds / 3600);
	seconds %= 3600;
	minutes = (int) (seconds / 60);
	seconds %= 60;

	snprintf(buf, sizeof(buf), "%d day%s, %d:%02d:%02lu",
			days, (days == 1) ? "" : "s", hours,
			minutes, (unsigned long) seconds);

	return buf;
}




/* dlink stuff is stolen from ircd-ratbox, original header:
 *  Copyright (C) 1996-2002 Hybrid Development Team
 *  Copyright (C) 2002 ircd-ratbox development team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 * dlink_ routines are stolen from squid, except for dlinkAddBefore,
 * which is mine.
 *   -- adrian
 */
dlink_node *
make_dlink_node(void)
{
	dlink_node *lp = my_calloc(1, sizeof(dlink_node));
	lp->next = lp->prev = NULL;
	return lp;
}

void
free_dlink_node(dlink_node *lp)
{
	my_free(lp);
}

void
dlink_add(void *data, dlink_node *m, dlink_list *list)
{
	m->data = data;
	m->next = list->head;
	m->prev = NULL;

	/* Assumption: If list->tail != NULL, list->head != NULL */
	if (list->head != NULL)
		list->head->prev = m;
	else if(list->tail == NULL)
		list->tail = m;

	list->head = m;
	list->length++;
}

void
dlink_add_tail(void *data, dlink_node *m, dlink_list *list)
{
	m->data = data;
	m->next = NULL;
	m->prev = list->tail;

	/* Assumption: If list->tail != NULL, list->head != NULL */
	if (list->tail != NULL)
		list->tail->next = m;
	else if(list->head == NULL)
		list->head = m;

	list->tail = m;
	list->length++;
}

void
dlink_add_before(void *data, dlink_node *m, dlink_node *pos, dlink_list *list)
{
	dlink_node *prev;

	if (pos)
	{
		prev = pos->prev;

		if (prev)
		{
			m->data = data;

			prev->next = m;
			m->prev = prev;

			pos->prev = m;
			m->next = pos;

			list->length++;
		}
		/* adding to front of list, shortcut this */
		else
			dlink_add(data, m, list);
	}
	/* adding to tail of list, shortcut this too */
	else
		dlink_add_tail(data, m, list);
}

void
dlink_delete(dlink_node *m, dlink_list *list)
{
	/* Assumption: If m->next == NULL, then list->tail == m
	 *      and:   If m->prev == NULL, then list->head == m
	 */
	if (m->next)
		m->next->prev = m->prev;
	else
		list->tail = m->prev;

	if (m->prev)
		m->prev->next = m->next;
	else
		list->head = m->next;

	/* Set this to NULL does matter */
	m->next = m->prev = NULL;
	list->length--;
}

dlink_node *
dlink_find(void *data, dlink_list *list)
{
	dlink_node *ptr;

	DLINK_FOREACH(ptr, list->head)
	{
		if (ptr->data == data)
			return (ptr);
	}
	return (NULL);
}

void
dlink_move_list(dlink_list *from, dlink_list *to)
{
	/* There are three cases */
	/* case one, nothing in from list */
	if (from->head == NULL)
		return;

	/* case two, nothing in to list */
	if (to->head == NULL)
	{
		to->head = from->head;
		to->tail = from->tail;
		from->head = from->tail = NULL;
		to->length = from->length;
		from->length = 0;
		return;
	}

	/* third case play with the links */
	from->tail->next = to->head;
	to->head->prev = from->tail;
	to->head = from->head;
	from->head = from->tail = NULL;
	to->length += from->length;
	from->length = 0;
}

void
dlink_move_list_tail(dlink_list * from, dlink_list * to)
{
	/* There are three cases */
	/* case one, nothing in from list */
	if (from->head == NULL)
		return;

	/* case two, nothing in to list */
	if (to->head == NULL)
	{
		to->head = from->head;
		to->tail = from->tail;
		from->head = from->tail = NULL;
		to->length = from->length;
		from->length = 0;
		return;
	}

	/* third case play with the links */
	from->head->prev = to->tail;
	to->tail->next = from->head;
	to->tail = from->tail;
	to->length += from->length;
	from->head = from->tail = NULL;
	from->length = 0;
}

dlink_node *
dlink_find_delete(void *data, dlink_list *list)
{
	dlink_node *m;

	DLINK_FOREACH(m, list->head)
	{
		if (m->data != data)
			continue;

		if (m->next)
			m->next->prev = m->prev;
		else
			list->tail = m->prev;

		if (m->prev)
			m->prev->next = m->next;
		else
			list->head = m->next;

		m->next = m->prev = NULL;
		list->length--;
		return m;
	}
	return NULL;
}

int
dlink_find_destroy(void *data, dlink_list *list)
{
	void *ptr = dlink_find_delete(data, list);
	if (ptr != NULL)
	{
		free_dlink_node(ptr);
		return 1;
	}
	return 0;
}


void
dlink_move_node(dlink_node *m, dlink_list *oldlist, dlink_list *newlist)
{
	/* Assumption: If m->next == NULL, then list->tail == m
	 *      and:   If m->prev == NULL, then list->head == m
	 */
	if (m->next)
		m->next->prev = m->prev;
	else
		oldlist->tail = m->prev;

	if (m->prev)
		m->prev->next = m->next;
	else
		oldlist->head = m->next;

	m->prev = NULL;
	m->next = newlist->head;
	if (newlist->head != NULL)
		newlist->head->prev = m;
	else if(newlist->tail == NULL)
		newlist->tail = m;
	newlist->head = m;

	oldlist->length--;
	newlist->length++;
}

